package selenium;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class 拼接补全帐号 {
//网易基本成型          帐号文件为E:\\ZhangHao/娱乐号.txt
	public static void main(String[] args) throws IOException {
		String zhangHaofilepath = "D:\\3712";// 帐号文件夹路径
		String zhangHaosName = "陶林帐号cookie拼接后 - 副本";// 帐号文件夹路径
		Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
		Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
		
		Date now = new Date();// 获取系统当前时间
		SimpleDateFormat sDate = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		String strDate = sDate.format(now);// 格式化日期转换为字符串
		
		final WebDriver driver = new ChromeDriver();
		//driver.manage().window().maximize();
		driver.get("http://mp.163.com/wemedia/baseinfo/isexist/5");
		try {
				for (int z = 0; z < 99999; z++) {
				driver.manage().deleteAllCookies();

				SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy hh:mm:ss a",Locale.US);
				
				String name = "NTES_SESS";
				// 读取帐号文件
				List<String> zhanghus = readFileAll(zhangHaofilepath + "/" + zhangHaosName + ".txt");
				String line2 = zhanghus.get(0).trim();
				String[] s1 = line2.split("\\/", -1);
				String value = s1[1];
				System.out.println("---------------start---------------------");
				System.out.println(s1[0]+"----开始执行巡查任务");
				
				String domain = ".mp.163.com";
				String path = "/";
				Date expiry = sdf.parse("1/30/2028 10:31:37 AM");
				Cookie cookie = new Cookie(name, value, domain, path, expiry);
				driver.manage().addCookie(cookie);	
				Thread.sleep(400);
				driver.get("http://mp.163.com/info.html");
				Thread.sleep(400);
				driver.navigate().refresh();
				Thread.sleep(400);
				
				StringBuffer s = new StringBuffer();
				s.append(s1[0]+"/");
				// ------start--帐号分类--邮箱帐号--
				
				WebElement leiDriver = driver.findElement(By.xpath("//*[@id='accountForm']/div[3]/div/p"));
				String fenLei = leiDriver.getText().toString().trim();
				System.out.println("分类：" + fenLei);
				s.append(fenLei + "/");

				WebElement mingChengDriver = driver.findElement(By.xpath("//*[@id='accountForm']/div[1]/div[1]/p"));
				String mingCheng = mingChengDriver.getText().toString().trim();
				System.out.println("帐号名称：" + mingCheng);
				s.append(mingCheng + "/");
				
				WebElement wemediaIdElement = driver.findElement(By.xpath("//*[@id='loginUser']"));//loginUser
				String wemediaIdTemp = wemediaIdElement.getAttribute("href").toString();
				String wemediaId = wemediaIdTemp.replace("http://mp.163.com/wemedia/info.html?wemediaId=", "");
				s.append(wemediaId + "/");	
				
				s.append(s1[1] + "/");


				String zhangHaoString = s.toString();
				
				System.out.println(zhangHaoString);
				
				File fileExist = new File(zhangHaofilepath + "/" + strDate + ".txt");
				if (!fileExist.exists()) {
					fileExist.createNewFile();
				}
				method2(zhangHaofilepath + "/" + strDate + ".txt", zhangHaoString);
				// 删除视频第一行
				List<String> list = readFile(zhangHaofilepath + "/" + zhangHaosName + ".txt", line2);
				writeFile(zhangHaofilepath + "/" + zhangHaosName + ".txt", list);
				
				driver.manage().deleteAllCookies();
				
				System.out.println("------------------------------------------");
				System.out.println("还有("+list.size()+")个【帐号】没有转化");
				System.out.println("---------------end---------------------");
				}
				
		} catch (Exception e) {
		   e.printStackTrace();
		}
		 driver.quit();
	};




	// 方法1：
	public void method1() {
		FileWriter fw = null;
		try {
			// 如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File("E:\\dd.txt");
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println("追加内容");
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 方法2：
	public static void method2(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	// 方法3：
	public static void method3(String fileName, String content) {
		try {
			// 打开一个随机访问文件流，按读写方式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度，字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static List<String> readFileAll(String filePath) throws IOException {
		File file = new File(filePath);
		BufferedReader reader = null;
		List<String> list = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(file));
		String text = reader.readLine();
		while (text != null) {
			list.add(text.trim() + "\r\n");
			text = reader.readLine();
		}
		reader.close();
		return list;
	}

	public static List<String> readFile(String filePath, String str) throws IOException {
		File file = new File(filePath);
		BufferedReader reader = null;
		List<String> list = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(file));
		String text = reader.readLine();
		while (text != null) {
			if (!text.trim().equals(str)) {
				list.add(text.trim() + "\r\n");
			}
			text = reader.readLine();
		}
		reader.close();
		return list;
	}

	public static void writeFile(String filePath, List<String> list) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outputStream = new FileOutputStream(file);
		for (String s : list) {
			outputStream.write(s.getBytes());
		}
		outputStream.close();
	}
	
	public static void writeFileRN(String filePath, List<String> list) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outputStream = new FileOutputStream(file);
		for (String s : list) {
			outputStream.write((s+"\r\n").getBytes());
		}
		outputStream.close();
	}

}
